package com.example.project.controller;

import com.example.project.common.Result;
import com.example.project.service.UserService;
import com.example.project.dto.UpdatePasswordRequest;
import com.example.project.dto.UpdateProfileRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/profile")
    public Result<?> getProfile(@RequestAttribute("userId") Long userId) {
        log.info("获取个人信息: userId={}", userId);
        return userService.getProfile(userId);
    }

    @PutMapping("/profile")
    public Result<?> updateProfile(
            @RequestBody UpdateProfileRequest request,
            @RequestAttribute("userId") Long userId) {
        log.info("更新个人信息: userId={}, request={}", userId, request);
        return userService.updateProfile(userId, request);
    }

    @PutMapping("/password")
    public Result<?> updatePassword(
            @RequestBody UpdatePasswordRequest request,
            @RequestAttribute("userId") Long userId) {
        log.info("修改密码: userId={}", userId);
        return userService.updatePassword(userId, request);
    }

    @PostMapping("/avatar")
    public Result<?> updateAvatar(
            @RequestParam("file") MultipartFile file,
            @RequestAttribute("userId") Long userId) {
        log.info("更新头像: userId={}, fileName={}", userId, file.getOriginalFilename());
        return userService.updateAvatar(userId, file);
    }
} 